package com.crm.system.service.impl.workOrderManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.dao.mapper.messageprompt.MessagePromptMapper;
import com.crm.model.entity.messageprompt.MessagePrompt;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.UserInfo;
import com.crm.model.entity.workOrderManager.WorkOrder;
import com.crm.model.entity.workOrderManager.WorkOrderCreditDeductRecord;
import com.crm.model.entity.workOrderManager.WorkOrderReply;
import com.crm.model.qo.workOrderManager.WorkOrderQO;
import com.crm.model.vo.workOrderManager.WorkOrderReplyVO;
import com.crm.model.vo.workOrderManager.WorkOrderVO;
import com.crm.service.sys.UserAccountDbService;
import com.crm.service.sys.UserInfoDbService;
import com.crm.service.workOrderManager.WorkOrderDbService;
import com.crm.system.service.workOrderManager.WorkOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author baijian
 * @Date 2021/5/18 15:02
 */
@Service
public class WorkOrderServiceImpl implements WorkOrderService {

    @Autowired
    private WorkOrderDbService workOrderDbService;

    @Autowired
    private MessagePromptMapper messagePromptMapper;
    
    @Autowired
    private UserAccountDbService userAccountDbService;

    @Autowired
    private UserInfoDbService userInfoDbService;



    /**
     * 管理模块 -- 工单管理列表
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param sortName  排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param workOrderQO 工单QO
     * @return
     */
    @Override
    public DhPageInfo<WorkOrderVO> workOrderList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, WorkOrderQO workOrderQO) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        boolean rs = CommonUtils.isAdmin(userAccount.getUserId());


        Integer dateUnit = 0;
        if (!StringUtils.isBlank(workOrderQO.getDateType())) {
            if ("thisWeek".equals(workOrderQO.getDateType())) {
                //本周
                dateUnit = 1;
            }
            if ("thisMonth".equals(workOrderQO.getDateType())) {
                //本月
                dateUnit = 2;
            }
            if ("thisYear".equals(workOrderQO.getDateType())) {
                //本年
                dateUnit = 3;
            }
            if("me".equals(workOrderQO.getDateType())) {
                //我的
                dateUnit = 4;
            }
        }

        workOrderQO.setOwner(userAccount.getAccountId());

        List<WorkOrderVO> workOrderList = new ArrayList<WorkOrderVO>();
        if("0".equals(workOrderQO.getBs())) {
            if(rs) {
                //超级管理员
                workOrderQO.setNumber(0);
                workOrderList = workOrderDbService.workOrderList(workOrderQO, "wwo.create_tm desc",dateUnit);
            }else {
                //普通管理员
                workOrderQO.setNumber(1);
                workOrderQO.setName(userAccount.getCompanyId());
                workOrderList = workOrderDbService.workOrderList(workOrderQO, "wwo.create_tm desc",dateUnit);
            }
        }else {
            //普通用户
            workOrderQO.setNumber(2);
            workOrderQO.setName(userAccount.getAccountId());
            workOrderList = workOrderDbService.workOrderList(workOrderQO, "wwo.create_tm desc",dateUnit);
        }

        PageInfo<WorkOrderVO> pageInfo = new PageInfo<WorkOrderVO>(workOrderList);
        return new DhPageInfo<WorkOrderVO>(pageInfo, workOrderList);
    }

    /**
     * 工单管理-新增工单
     * @param workOrder 工单实体类
     * @return
     */
    @Override
    public String addWorkOrder(WorkOrder workOrder) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        UserInfo userInfo = userInfoDbService.selectUserInfoByUserId(userAccount.getUserId());
        if(userInfo.getCreditScore() < 96) {
            return "WO0002";
        }

        String workOrderId = CommonUtils.getGUID();
        workOrder.setWorkOrderId(workOrderId);
        workOrder.setWorkOrderNumber(String.valueOf(new Date().getTime()));
        workOrder.setCreatePeopleId(userAccount.getAccountId());
        workOrder.setCreateTm(new Date());
        workOrder.setUpdatePeopleId(userAccount.getAccountId());
        workOrder.setUpdateTm(new Date());
        workOrder.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        workOrder.setAccountId(userAccount.getAccountId());
        workOrder.setCompanyId(userAccount.getCompanyId());
        workOrderDbService.addWorkOrder(workOrder);
        return "SUCCESS";
    }

    /**
     * 工单管理-工单列表-回复工单
     * @param workOrderReply 工单实体类
     * @return
     */
    @Override
    public String addWorkOrderReply(WorkOrderReply workOrderReply) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();

        List<WorkOrderReplyVO> list = workOrderDbService.workOrderReplyList(workOrderReply.getWorkOrderId());
        if(list.size() == 10) {
            return "WO0001";
        }

        String workOrderReplyId = CommonUtils.getGUID();
        workOrderReply.setWorkOrderReplyId(workOrderReplyId);
        workOrderReply.setCreatePeopleId(userAccount.getAccountId());
        workOrderReply.setCreateTm(new Date());
        workOrderReply.setUpdatePeopleId(userAccount.getAccountId());
        workOrderReply.setUpdateTm(new Date());
        workOrderReply.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        Boolean result = workOrderDbService.addWorkOrderReply(workOrderReply);

        WorkOrder workOrder = workOrderDbService.getWorkOrderById(workOrderReply.getWorkOrderId());
        UserAccount account = userAccountDbService.selectUserAccountByAccountId(workOrder.getAccountId());


        MessagePrompt messagePrompt = new MessagePrompt();
        messagePrompt.setMessageId(CommonUtils.getGUID());
        messagePrompt.setMessageType((Short.valueOf("6")));
        messagePrompt.setMessageState((Short.valueOf("1")));
        messagePrompt.setMessageContent("您的工单编号为："+workOrder.getWorkOrderNumber()  +"有人回复---"+workOrder.getWorkOrderId()+"+++"+account.getUserName());
        messagePrompt.setRecipientAccountId(workOrder.getCreatePeopleId());
        messagePrompt.setCreatePeopleId(userAccount.getAccountId());
        messagePrompt.setCreateTm(new Date());
        messagePromptMapper.insertSelective(messagePrompt);
        if(result){
            return "SUCCESS";
        }else{
            return "FALSE";
        }

    }

    /**
     * 工单管理-工单列表-回复工单列表
     * @param workOrderId 工单id
     * @return
     */
    @Override
    public List<WorkOrderReplyVO> workOrderReplyList(String workOrderId) {
        return workOrderDbService.workOrderReplyList(workOrderId);
    }

    /**
     * 用户端-工单管理-工单详情
     * @param workOrderId 工单id
     * @return
     */
    @Override
    public WorkOrder getWorkOrderById(String workOrderId) {
        return workOrderDbService.getWorkOrderById(workOrderId);
    }

    /**
     * 修改工单
     * @Author renshaorong
     * @Date 2021/7/20
     * @param workOrder
     * @return
     */
    @Override
    public boolean updateByPrimaryKeySelective(WorkOrder workOrder){
        return workOrderDbService.updateByPrimaryKeySelective(workOrder);
    }

    /**
     * 工单管理-超级管理员-扣除信用值
     * @param workOrderCreditDeductRecord 工单扣除信用值记录
     * @return
     */
    @Override
    public Boolean creditDeduct(WorkOrderCreditDeductRecord workOrderCreditDeductRecord) {
        UserAccount userAccount = userAccountDbService.selectUserAccountByAccountId(workOrderCreditDeductRecord.getOwnerId());
        UserInfo userInfo = userInfoDbService.selectUserInfoByUserId(userAccount.getUserId());

        UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        workOrderCreditDeductRecord.setCreditDeductId(CommonUtils.getGUID());
        workOrderCreditDeductRecord.setCreatePeopleId(user.getAccountId());
        workOrderCreditDeductRecord.setCreateTm(new Date());
        workOrderCreditDeductRecord.setUpdatePeopleId(user.getAccountId());
        workOrderCreditDeductRecord.setUpdateTm(new Date());
        boolean rs = workOrderDbService.addWorkOrderCreditDeductRecord(workOrderCreditDeductRecord);
        if(rs) {
            userInfo.setCreditScore(userInfo.getCreditScore()-workOrderCreditDeductRecord.getDeductScore());
            userInfo.setUpdateUserId(user.getAccountId());
            userInfo.setUpdateTm(new Date());
            userInfoDbService.updateUserInfoByUserId(userInfo);
        }

        return rs;
    }
}
